import datetime
import requests

totalPages = 1

def timeFmt(timeFrom: str,dayNum: int = 0,):
    """计算机时区 +8 time zone 时间格式化 dayNum = 0 今天 -1 昨天 timeFrom eg: %Y-%m-%d %Y%m%d%H%M%S"""
    return (datetime.datetime.now() + datetime.timedelta(days=dayNum, hours=+8)).strftime(timeFrom)

def FileSave(filename,content:str):
    file =  open(filename,"w+",encoding="utf-8")
    file.write(content)
    file.close()


def GetAllWhispers():
    url = "https://api.ftls.xyz/api/collections/whispers/records?sort=-createdAt&perPage=100"
    resp = requests.get(url)
    obj = resp.json()
    totalPages = obj['totalPages']
    print(totalPages)
    for index in range(1 ,totalPages+1):
        filename = timeFmt("%Y%m_") + str(index) + ".json"
        resp = requests.get(url + "&page=" + str(index))
        obj = resp.text
        FileSave(filename,obj)

    # print(obj)

GetAllWhispers()

